package com.zhou.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * 跨域配置
 * @author Hao.
 * @version 1.0
 * @date 2021/6/20 10:34
 */
@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration configuration = new CorsConfiguration();
        // 运行所有头信息跨域
        configuration.addAllowedHeader("*");
        // 运行所有请求方法类型跨域
        configuration.addAllowedMethod("*");
        // 运行任意来源的跨域
        configuration.addAllowedOrigin("*");
        // 运行携带cookie跨域
        configuration.setAllowCredentials(true);

        source.registerCorsConfiguration("/**", configuration);
        return new CorsWebFilter(source);
    }
}
